#ifndef HTTP_CONNECTION_H
#define HTTP_CONNECTION_H

#include <sparrow/http_header_variant_set.h>
#include <sparrow/http_parser.h>
#include <sparrow/handler.h>

class Reactor;
class HttpConnectionManager;
class HttpConnection : public Handler
{
    public:
        enum State
        {
            ACCEPTED = 1,
            READ_REQUEST_HEADER,
            READ_REQUEST_BODY,
            READ_PIPELINE_REQUESTS,
            SERVING_REQUEST,
        };

        HttpConnection(Reactor *r, HttpConnectionManager *manager, VariantSet *connection_attributes);

        virtual void handle_input(int fd, uint64_t token);
        virtual void handle_output(int fd, uint64_t token);
        virtual void handle_error(int fd, uint64_t token);
        virtual void handle_timeout(uint64_t token);
        void once(VariantSet *v);

        int64_t get_token() const;
        void content_response(VariantSet *v);
        void error_response(VariantSet *v);

    private:
        void change_state(State new_state);
        void read_request_header(const Octets &input);
        void send_to_backend();

        State state_;
        bool ignore_timeout_;
        HttpHeaderVariantSet http_header_;
        HttpParser parser_;
        int64_t token_;
        VariantSet *connection_attributes_;
        HttpConnectionManager *manager_;
        int32_t server_fd_;
};

#endif // HTTP_CONNECTION_H

